package com.cskaoyan.service;

import com.cskaoyan.bean.BaseData;
import com.cskaoyan.bean.BaseRespVo;
import com.cskaoyan.bean.goods.wxbean.GoodsDetailVO;
import com.cskaoyan.bean.promotion.topic.Topic;
import com.cskaoyan.bean.promotion.topic.TopicExample;
import com.cskaoyan.bean.promotion.topic.TopicVO;
import com.cskaoyan.bean.wxtopic.GoodsInfo;
import com.cskaoyan.bean.wxtopic.TopicDetailVO;
import com.cskaoyan.bean.wxtopic.WXTopicParam;
import com.cskaoyan.bean.wxtopic.WxTopicVo;
import com.cskaoyan.mapper.TopicMapper;
import com.cskaoyan.mapper.WxTopicMapper;
import com.cskaoyan.mapper.goodsMapper.MarketGoodsMapper;
import com.cskaoyan.util.PageUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * \* Description:
 * \
 */
@Service
public class WxTopicServiceImpl implements WxTopicService {


    @Autowired
    MarketGoodsMapper marketGoodsMapper;
    @Autowired
    TopicMapper topicMapper;

    @Override
    public BaseData list(Integer limit, Integer page) {
        PageHelper.startPage(page, limit);
        TopicExample topicExample = new TopicExample();

        List<Topic> topics = topicMapper.selectByExample(topicExample);
        ArrayList<WxTopicVo> wxTopicVos = new ArrayList<>();

        for (Topic topic : topics) {
            WxTopicVo wxTopicVo = new WxTopicVo();
            wxTopicVo.setId(topic.getId());
            wxTopicVo.setTitle(topic.getTitle());
            wxTopicVo.setSubtitle(topic.getSubtitle());
            wxTopicVo.setReadCount(topic.getReadCount());
            wxTopicVo.setPicUrl(topic.getPicUrl());
            wxTopicVos.add(wxTopicVo);
        }

        PageInfo<WxTopicVo> wxTopicVoPageInfo = new PageInfo<>(wxTopicVos);
        BaseData<WxTopicVo> wxTopicVoBaseData = new BaseData<>(wxTopicVoPageInfo, wxTopicVos);


        return wxTopicVoBaseData;
    }

    @Override
    public TopicDetailVO detail(int id) {

        TopicVO topicVO = topicMapper.getTopicdetail(id);
        List<GoodsInfo> goodsInfoList = null;
        Integer[] goodsId = topicVO.getGoods();
        for (Integer goodId : goodsId) {
            goodsInfoList = marketGoodsMapper.getGoodsInfoBygoodId(goodId);
        }
        TopicDetailVO topicDetailVO = new TopicDetailVO(topicVO, goodsInfoList);
        return topicDetailVO;
    }

    @Override
    public BaseData<TopicVO> related(int id) {

         List<TopicVO>  topicVOS = topicMapper.getRelated(id);
        PageUtil.start(new WXTopicParam());
        PageInfo<TopicVO> pageInfo = new PageInfo<>(topicVOS);
        BaseData<TopicVO> data = new BaseData<>(pageInfo,topicVOS);
        return data;
    }
}
